/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.models.parking;

import app.models.info.Floor;
import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * <p>
 * The table olb_parking mapping model.
 * </p>
 */
@TableBind(tableName = "olb_parking")
public class Parking extends Model<Parking> {

    /**
     * The public dao.
     */
    public static final  Parking dao              = new Parking();
    private static final long    serialVersionUID = 1L;

    public List<Parking> list() {
        return find(SqlKit.sql("parking.list"));
    }

    public Page<Parking> datatable(DatatablesCriterias criterias, int floor) {

        int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();
        String where = " FROM olb_parking p INNER JOIN olb_floor f ON f.id = p.floor ";
        if (floor > 0) {
            where += " WHERE p.floor =? ";
        }
        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            // todo mulite column.
            final String byColumns = orderBy.toString();
            if (!Strings.isNullOrEmpty(byColumns)) {
                where += " ORDER BY " + byColumns;
            }
        }
        if (!where.contains("ORDER")) {
            where += " ORDER BY floor";
        }
        if (floor > 0) {

            return paginate(start, pageSize, SqlKit.sql("parking.columns"), where, floor);
        } else {

            return paginate(start, pageSize, SqlKit.sql("parking.columns"), where);
        }
    }


    public void batchWithFloor(Floor floor) {
        int floor_val = floor.getNumber(AppFunc.TABLE_PK_COLUMN).intValue();
        Object[][] batch_params = new Object[100][];
        Object[] param;
        for (int i = 0; i < 100; i++) {
            String parking_name = floor.getStr("name") + String.format("%03d", i + 1);
            param = new Object[3];
            param[0] = floor_val;
            param[1] = parking_name;
            param[2] = floor.getNumber("mall").intValue();
            batch_params[i] = param;
        }

        Db.batch(SqlKit.sql("parking.batch"), batch_params, 100);

    }

    public Parking findbyLocation(String parking_no, int type) {
        if (type == 1) {
            // b2 floor
            return findFirst(SqlKit.sql("parking.findbyLocationAndB2"), parking_no);
        } else {
            return findFirst(SqlKit.sql("parking.findbyLocation"), parking_no);
        }
    }
}